package frontOffice;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rai.model.Personne;
import rai.service.rep.ListeRepertoires;
import rai.service.rep.Repertoire;

/**
 * Servlet implementation class ManageRepertoire
 */
public class ManageRepertoire extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ManageRepertoire() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		ListeRepertoires lesRepertoires = (ListeRepertoires) session
				.getAttribute("laConnexion");
		String login = (String) session.getAttribute("leLogin");
		String identifiantRepertoire = request.getParameter("nomRep");
		String messagePers = "";
		String messageRep = "";
		Repertoire leRepertoire = null;
		String identifiantPersonne = null;
		boolean estUnRepertoire;
		String actionPers = (request.getParameter("actionPers") == null) ? "noActionPers"
				: request.getParameter("actionPers");
		String[] listeRepertoires = null;
		String[] listePersonnes = null;
		String actionRep;
		Personne lapersonne=null;
		boolean reponse = false;
		
		// action repertoire
		actionRep = (request.getParameter("actionRep") == null) ? "noactionRep"
				: request.getParameter("actionRep");
		String nomRep = request.getParameter("nomRep");
		if (actionRep.equals("supprimer")) {
			reponse = lesRepertoires.supprimerRepertoire(nomRep);
			if (reponse)
				messageRep = "le repertoire " + nomRep + " supprime";
			else
				messageRep = "le repertoire " + nomRep + " n'existe pas";
		}

		if (actionRep.equals("ajouter")) {
			reponse = lesRepertoires.creerRepertoire(nomRep);
			leRepertoire = lesRepertoires.rechercherRepertoire(nomRep);
			if (reponse)
				messageRep = "le repertoire " + nomRep + " ajoute";
			else
				messageRep = "le repertoire " + nomRep + " existe deja";
		}
		if (actionRep.equals("sauver")) {
			reponse = lesRepertoires.sauverListeRepertoires();
			leRepertoire = lesRepertoires.rechercherRepertoire(nomRep);
			if (reponse)
				messageRep = "Sauvegarde reussite";
			else
				messageRep = "Echec sauvegarde";
		}
		if (actionRep.equals("quitter")) {
			// session.invalidate();
			retourPageDAcceuil(request, response);
		}
		if (identifiantRepertoire == null) {
			identifiantRepertoire = "";
		} else {
			leRepertoire = lesRepertoires
					.rechercherRepertoire(identifiantRepertoire);
			identifiantPersonne = request.getParameter("personne");
		}
		estUnRepertoire = leRepertoire != null;
		if (estUnRepertoire) {
			if (identifiantPersonne == null) {
				lapersonne = new Personne("", "", "", "");
			} else {
				lapersonne = leRepertoire.chercherPersonne(identifiantPersonne);
			}
			// action personne
			String nom = request.getParameter("nom");
			if (actionPers.equals("supprimer")) {
				reponse = leRepertoire.retirerPersonne(nom);
				if (reponse)
					messagePers = "la personne " + nom + " supprimee";
				else
					messagePers = "le personne " + nom + " n'existe pas";
			}
			if (actionPers.equals("ajouter")) {
				lapersonne = new Personne(nom, request.getParameter("email"),
						request.getParameter("url"), request
								.getParameter("info"));
				reponse = leRepertoire.ajouterPersonne(lapersonne);
				if (reponse)
					messagePers = "la personne " + nom + " ajoutee";
				else
					messagePers = "le personne " + nom + " existe deja";
			}
			if (actionPers.equals("modifier")) {
				lapersonne = new Personne(nom, request.getParameter("email"),
						request.getParameter("url"), request
								.getParameter("info"));
				reponse = leRepertoire.modifierPersonne(lapersonne);
				if (reponse)
					messagePers = "la personne " + nom + " modifiee";
				else
					messagePers = "le personne " + nom + " n'existe pas";
			}
			listePersonnes = leRepertoire.listerPersonnes();
		}

		listeRepertoires = lesRepertoires.listerRepertoires();

		try {
			PrintWriter out = response.getWriter();

			try {
				response.setContentType("text/html");
				out
						.println("<HTML>\n<HEAD>  <link href=\"style"
								+ "/"
								+ "style.css\" rel=\"stylesheet\" type=\"text / css\" /></HEAD>");
				out.println("<BODY>\n" + "<div id=\"entete\"><H1>"
						+ "REPERTOIRE : " + identifiantRepertoire
						+ " </H1><br><br>" + login
						+ " est connecté sur la machine </div>");
				out.println("<div id=\"contenue\">");
				out
						.println("<div id=\"repertoires\"><H2>Listes des repertoires </H2>"
								+ "<div id=\"liste\">"
								+ "Il y a "
								+ listeRepertoires.length
								+ " repertoire(s)<br>");
				for (String repertoire : listeRepertoires) {
					out.println("<LI><a href=\"ManageRepertoire?nomRep="
							+ repertoire + "\">" + repertoire + "</a></LI>");
				}
				out
						.println("</div>"
								+ "<FORM method=get action=\"ManageRepertoire\">"
								+ "<div id=\"boutonsRepertoire\"> <p>"
								+ "<input type=\"submit\" name=\"actionRep\" value=\"ajouter\">"
								+ "<input type=\"submit\" name=\"actionRep\" value=\"supprimer\">"
								+ "<input type=\"submit\" name=\"actionRep\" value=\"sauver\"></p></div>"
								+ "<input type=\"submit\" name=\"actionRep\" value=\"quitter\"></p></div>"
								+ messageRep
								+ "<div id=\"tableau\"><TABLE border=\"1\">"
								+ "<TR>"
								+ "<TD>nom :</TD><TD><INPUT type=text name=\"nomRep\" value=\""
								+ identifiantRepertoire + "\"/></TD>"
								+ "<TR></TABLE></FORM></div>");
				if (estUnRepertoire) {
					out.println("<div id=\"personne\">"
							+ "<H2>Listes des personnes</H2><div id=\"liste\">"
							+ "Il y a " + listePersonnes.length
							+ " personne(s.");
					for (String personne : listePersonnes)
						out.println("<LI><a href=\"ManageRepertoire?nomRep="
								+ identifiantRepertoire + "&personne="
								+ personne + "\">" + personne + "</a></LI>");
					out
							.println("</div>"
									+ "<FORM method=get action=\"ManageRepertoire\">"
									+ "<input type=\"hidden\" name=\"nomRep\" value=\""
									+ identifiantRepertoire
									+ "\">"
									+ "<div id=\"boutonsPersonne\"> <p>"
									+ "<input type=\"submit\" name=\"actionPers\" value=\"ajouter\">"
									+ "<input type=\"submit\" name=\"actionPers\" value=\"supprimer\">"
									+ "<input type=\"submit\" name=\"actionPers\" value=\"modifier\"></p></div>"
									+ "<div id=\"tableau\">"
									+ messagePers
									+ "<TABLE border=\"1\">"
									+ "<TR>"
									+ "<TD>nom :</TD><TD><INPUT type=text name=\"nom\" value=\""
									+ lapersonne.getNom()
									+ "\"></TD><BR>"
									+ "</TR><TR>"
									+ "<TD>email :</TD><TD><INPUT type=text name=\"email\" value=\""
									+ lapersonne.getEmail()
									+ "\"></TD><BR>"
									+ "</TR><TR>"
									+ "<TD>url :</TD><TD><INPUT type=text name=\"url\" value=\""
									+ lapersonne.getUrl()
									+ "\"></TD><BR>"
									+ "</TR><TR>"
									+ "<TD>info :</TD><TD><textarea name=\"info\" rows=\"5\" cols=\"16\">"
									//+ "<TD>url :</TD><TD><INPUT type=text name=\"info\" value=\""
									+ lapersonne.getInfo()
									+ "</textarea></TD><BR>"
									+ "</TR></TABLE>" + "</div></FORM></div>");
				}
				out.println("</div></BODY></HTML>");

			} finally {
				out.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void retourPageDAcceuil(HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			response.setContentType("text/html");
			out
					.println("<HTML>\n<HEAD>  <link href=\"style/style.css\" rel=\"stylesheet\" type=\"text / css\" /></HEAD>");
			out
					.println("<BODY onload=\"alert('Sortie Application');window.location.replace('index')\">");
			out.println("<br></BODY></HTML>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			out.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
